package com.it.gw.modules.station.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.GlideUtils
import com.it.data.network.rsp.FileVo
import com.it.gw.R
import com.it.gw.databinding.ItemPicReportBinding


class SelectFileAdapter(
    var context: Context,
    var listener: ((FileVo) -> Unit)? = null,
    var delete: ((FileVo) -> Unit)? = null,
    var datas: List<FileVo>
) : RecyclerView.Adapter<SelectFileAdapter.ViewHolder>() {
    companion object {
        const val TAG = "SelectFileAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemPicReportBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        if (data.path.isNullOrEmpty()) {
            GlideUtils.loadRes(context, R.drawable.icon_add_pic, holder.binding.ivPic)
            holder.binding.ivClose.visibility = View.GONE
        } else {
            GlideUtils.loadFile(context, data.path, holder.binding.ivPic)
            holder.binding.ivClose.visibility = View.VISIBLE
        }

        holder.binding.ivClose.setOnClickListener {
            delete?.invoke(data)
        }
        holder.binding.ivPic.setOnClickListener {
            listener?.invoke(data)
        }


    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemPicReportBinding) : RecyclerView.ViewHolder(binding.root)
}